﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1
{
    class Input:IGlobalInput,IGameInput
    {
        PlayerIndex _index;
        public Input(PlayerIndex playerIndex)
        {
            _index = playerIndex;

            _upBindings.Add(() =>
                                {
                                    var state = GetGamepadState();
                                    return state.DPad.Up == ButtonState.Pressed
                                           || state.ThumbSticks.Left.Y > 0;
                                });
            _upBindings.Add(() =>
                                {
                                    var state = GetKeyboardState();
                                    return state.IsKeyDown(Keys.Up) || state.IsKeyDown(Keys.W);
                                });

            _downBindings.Add(() =>
            {
                var state = GetGamepadState();
                return state.DPad.Down == ButtonState.Pressed
                    || state.ThumbSticks.Left.Y < 0;
            });
            _downBindings.Add(() =>
                                  {
                                      var state = GetKeyboardState();
                                      return state.IsKeyDown(Keys.Down) || state.IsKeyDown(Keys.S);
                                  });

        }

        readonly List<Func<bool>> _downBindings = new List<Func<bool>>()
            {
                                                              
                ()=>Keyboard.GetState().IsKeyDown(Keys.Down),
                ()=>Keyboard.GetState().IsKeyDown(Keys.S),
            };

        private readonly List<Func<bool>> _upBindings = new List<Func<bool>>();
        public List<Func<bool>> DownBindings {get { return _downBindings; }}
        
        KeyboardState GetKeyboardState()
        {
            return Keyboard.GetState(_index);
        }
        GamePadState GetGamepadState()
        {
            return GamePad.GetState(_index);
        }



        #region IGlobalInput Members

        public bool Back
        {
            get
            {
                return GetKeyboardState().IsKeyDown(Keys.Escape) ||
                       GetGamepadState().Buttons.Back == ButtonState.Pressed;
            }
        }

        public bool Quit
        {
            get {
                var keystate = GetKeyboardState();
                return GetKeyboardState().IsKeyDown(Keys.LeftAlt) && GetKeyboardState().IsKeyDown(Keys.F4);
            }
        }

        public bool Up
        {
            get { return _upBindings.Any(b => b()); }
        }

        public bool Down
        {
            get { return _downBindings.Any(b => b()); }
        }

        public bool Activate
        {
            get { return GetKeyboardState().IsKeyDown(Keys.Enter) || GetGamepadState().Buttons.A == ButtonState.Pressed; }
        }

        #endregion

        #region IGameInput Members

        public bool Pause
        {
            get { return GetGamepadState().Buttons.Start== ButtonState.Pressed || GetKeyboardState().IsKeyDown(Keys.Pause); }
        }

        #endregion
    }
}
